package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.AppComplaintDaoI;
import sy.model.AppComplaint;
import sy.model.AppEquipment;
@Repository("appComplainDao")
public class AppComplaintDaoImpl implements AppComplaintDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public Serializable save(AppComplaint appComplaint) {
		// TODO Auto-generated method stub
		return this.getCurrentSession().save(appComplaint);
	}

	@Override
	public List<AppComplaint> listAppComplaintBySysAccountId(String sysAccountId) {
		// TODO Auto-generated method stub
		String hql = "from AppComplaint where sysAccountId = :sysAccountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("sysAccountId", sysAccountId);
		return query.list();
	}
	
	@Override
	public List<AppComplaint> getList() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from AppComplaint").list();
	}

	@Override
	public void updateStatusByCode(String code, String status) {
		// TODO Auto-generated method stub
		String hql = "update AppComplaint set status = :status where code = :code";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("status", status);
		query.setString("code", code);
		query.executeUpdate();
	}

	@Override
	public AppComplaint getAppComplaintByCode(String code) {
		// TODO Auto-generated method stub
		String hql = "from AppComplaint where code = :code";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("code", code);
		List<AppComplaint> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<AppComplaint> getListByStatus(String status) {
		// TODO Auto-generated method stub
		String hql = "from AppComplaint where status = :status ";
		if("0".equals(status)){
			hql += "order by createTime desc";
		}else{
			hql += "order by manageTime desc";
		}
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("status", status);
		List<AppComplaint> list = query.list();
		return list;
	}

}
